package com.yywz.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXTest {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, MalformedURLException, IOException {
		
		String url;
		
		if (args.length == 0) {
			url = "http://www.w3.org";
		} else {
			url = args[0];
		}

		DefaultHandler handler = new DefaultHandler() {
			public void startElement(String namespaceURI, String lname, String qname, Attributes attrs) {
				if (lname.equals("a") && attrs != null) {
					for (int i = 0; i < attrs.getLength(); i++) {
						String name = attrs.getLocalName(i);
						if (name.equals("href"))
							System.out.println(attrs.getValue(i));
					}
				}
			}
		};

		SAXParserFactory factory = SAXParserFactory.newInstance();

		factory.setNamespaceAware(false);

		SAXParser saxParser = factory.newSAXParser();
		
//        HttpURLConnection   con   =   (HttpURLConnection)   (SourceURL).openConnection(); 
//        con.setRequestProperty( "Proxy-Authorization ",  "Basic   "   +   new   BASE64Encoder().encode( "username:password ".getBytes()));
		URL urlObject = new URL(url);
		
		System.setProperty("http.proxyHost", "192.168.1.8");
		System.setProperty("http.proxyPort", "80");
		
//		HttpURLConnection uc = (HttpURLConnection)urlObject.openConnection();


		InputStream in = urlObject.openStream();

		saxParser.parse(in, handler);

	}

}
